package com.lagou.edu.service.impl;

import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.factory.annotation.Autowired;
import com.lagou.edu.factory.annotation.Service;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;

@Service
public class TransferServiceImpl  implements TransferService {

    @Autowired
    private AccountDao accountDao ;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

//        try {
            // 开启事务
            //ConnectionUtils.getInstance().getCurrentThreadConn().setAutoCommit(false);
//            TransactionManager.getInstance().beginTransaction();

            System.out.println("执行转账逻辑");
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney() - money);
            to.setMoney(to.getMoney() + money);

            accountDao.updateAccountByCardNo(from);
             int c = 1/0;
            accountDao.updateAccountByCardNo(to);

            // 提交事务
            //ConnectionUtils.getInstance().getCurrentThreadConn().commit();
         /*   TransactionManager.getInstance().commit();
      }catch (Exception e){
            e.printStackTrace();
            TransactionManager.getInstance().rollback();
            throw e;
        }*/

    }
}
